package com.jxau.mall.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxau.mall.user.entity.UserReceiveAddress;
import com.jxau.mall.user.mapper.UserReceiveAddressMapper;
import com.jxau.mall.user.model.UserReceiveAddressDTO;
import com.jxau.mall.user.service.IUserReceiveAddressService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: shop
 * @description: user_receive_address表接口的实现类
 * @author: guojiani
 * @create: 2025-04-24 14:43
 **/

@Service
public class UserReceiveAddressServiceImpl extends ServiceImpl<UserReceiveAddressMapper, UserReceiveAddress> implements IUserReceiveAddressService {
    @Override
    public List<UserReceiveAddressDTO> listByUserId(Long userId) {
        // 构建条件
        LambdaQueryChainWrapper<UserReceiveAddress> queryChainWrapper = new LambdaQueryChainWrapper<>(baseMapper);
        List<UserReceiveAddress> list = queryChainWrapper.eq(UserReceiveAddress::getUserId, userId).list();
        // 对象转换
        if(list != null && list.size() > 0){
            List<UserReceiveAddressDTO> collect = list.stream()
                    // 将UserReceiveAddress对象转换JSON字符串,再将JSON字符串转换成UserReceiveAddressDTO对象
                    .map(e -> JSON.parseObject(JSON.toJSONString(e), UserReceiveAddressDTO.class))
                    // 收集成List
                    .collect(Collectors.toList());
            return collect;
        }
        return null;
    }
}
